<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <#include "../common/header.ftl">


    <script type="text/javascript">
        $(function () {
            $.get('/region/getByParentId.do',function (data) {
                $('#tree').treeview({
                    data:[{text:'全部地区',nodes:data}],
                    showTags:true,//开启标签显示
                    lazyLoad:function (node) {//懒加载
                        //查询子节点地区数据
                        $.get('/region/getByParentId.do',{id:node.id},function (list) {
                            //第一个参数是要添加的节点,第二个参数指定添加导那个(父节点)
                            $('#tree').treeview('addNode',[list,node]);
                        })
                    },
                    onNodeSelected: function (event,data) {//选择节点事件
                        //查询子节点数据
                        $.get('/region/listChildRegion.do',{id:data.id},function (list) {
                            var temp = "";
                            $.each(list,function (index,ele) {
                                var btnText = "设为推荐";
                                if (ele.state==1){
                                    btnText = "取消推荐";
                                }
                                temp += "<tr><td>"+(index+1)+"</td><td>"+ele.name+"</td>"
                                        +'<td><a href="javascript:void(0);" class="editBtn" data-json='+ele.jsonString+'>修改</a></td>'
                                        +'<td><a href="javascript:void(0);" class="changeStateBtn" data-json='+ele.jsonString+'>'+btnText+'</a></td>'
                                        +"</tr>";
                            })
                            $("tbody").html(temp);
                            //编辑按钮绑定点击事件
                            $(".editBtn").click(function () {
                                $("#editForm")[0].reset();
                                var json = $(this).data('json');
                                $("#regionId").val(json.id);
                                $("#name").val(json.name);
                                $("#parentId").val(json.parentId);
                                $("#parentName").val(json.parentName);
                                $("#regionModal").modal('show');
                            })
                            //修改状态按钮绑定点击事件
                            $(".changeStateBtn").click(function () {
                                var json = $(this).data('json');
                                var temp=0;//普通
                                if(json.state==0){
                                    temp=1;//推荐
                                }
                                $.post('/region/updateState.do',{id:json.id,state:temp},function (data) {
                                    showMessager(data);
                                })
                            })

                        })
                    }

                });
            })
            //保存按钮
            $("#saveBtn").click(function () {
                $("#editForm").ajaxSubmit(function (data) {
                    showMessager(data);
                });
            })

            //新增按钮
            $(".addBtn").click(function () {
                $("#editForm")[0].reset();
                //获取选中的地区的数组
                var arr = $('#tree').treeview('getSelected');
                if(arr.length>0){
                    $("#parentId").val(arr[0].id);
                    $("#parentName").val(arr[0].text);
                }
                $("#regionModal").modal('show');
            })


        })

    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
        <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                </div>
            </div>
            <div class="form-group">
                <a class="btn btn-success addBtn">添加</a>
            </div>
            <div class="row">
                <div class="col-sm-4">
                        <div id="tree"></div>

                </div>
                <div class="col-sm-8">
                    <table class="table table-striped table-hover" >
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>地区名称</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>

                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="regionModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">地区新增/编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm" >
                    <input type="hidden"  id="regionId" name="id">
                    <div class="form-group" >
                        <label for="name" class="col-sm-4 control-label">地区名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name"  >
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">上级：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="parentName"  readonly >
                            <input type="hidden" class="form-control" id="parentId" name="parent.id"   >
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary " id="saveBtn">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->



</body>
</html>